oPlayer = oUF:NewModule("oUF_Player", oUF.unit.player)
local G = getfenv(0)

--oUF.unit.player = oUF.class.unit:new("player")
--oUF:NewModule("oUF_Player", oUF.unit.player)

function oPlayer:Enable()
	self:disableBlizzard()
	oUF.unit.player = oUF.class.unit:new("player")
	oUF.unit.player:loadPosition()

	oUF.unit.player.energy = UnitMana('player')
	oUF.unit.player.ticktime = 2.0

	self:RegisterEvent("PLAYER_REGEN_ENABLED", "updateCombat")
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "updateCombat")
	self:RegisterEvent("PLAYER_UPDATE_RESTING", "updateCombat")
	self:RegisterEvent("UNIT_ENERGY", "startTick")
end

function oPlayer:createEnergyTick()
	local c = select(2, UnitClass("player"))

	if(c == "ROGUE" or c == "DRUID") then
		oUF.unit.player:SetScript("OnUpdate", self.OnUpdate)
		oUF.unit.player.Tick = oUF.unit.player.Power:CreateTexture(nil, "OVERLAY")
		oUF.unit.player.Tick:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
		oUF.unit.player.Tick:SetWidth(16)
		oUF.unit.player.Tick:SetHeight(30)
		oUF.unit.player.Tick:SetBlendmode("ADD")

		oUF.unit.player.var = {
		}
	end
end

function oPlayer:updateCombat()
	if(UnitAffectingCombat("player")) then
		oUF.unit.player:SetBackdropBorderColor(.8, .3, .22)
	elseif(IsResting()) then
		oUF.unit.player:SetBackdropBorderColor(.6, .6, 1)
	else
		oUF.unit.player:SetBackdropBorderColor(.3, .3, .3)
	end
end

--[[ Based upon kEnergy by kergoth
	SVN: http://svn.wowace.com/wowace/trunk/kEnergy/
	ZIP: http://wowace.com/files/kEnergy/
]]
local prevtime

function oPlayer:startTick(event, unit)
	local c = select(2, UnitClass("player"))
	if(unit ~= "player") then return end
	if(c == "ROGUE" or c == "DRUID") then
		local time = GetTime()
		local maxen = UnitManaMax(unit)
		local olden = oUF.unit.player.energy

		oUF.unit.player.energy = UnitMana(unit)
		if oUF.unit.player.energy ~= maxen and (oUF.unit.player.energy - olden ~= 20 or oUF.unit.player.energy - olden ~= 19 or oUF.unit.player.energy - olden ~= 21) then
			return
		end

		if(not oUF.unit.player.Tick) then self:createEnergyTick() end

		-- Update our weighted average of energy tick time
		if prevtime and oUF.unit.player.energyticking then
			-- there was a previous tick, and it was within this group of ticks
			oUF.unit.player.ticktime = ((99.0 * oUF.unit.player.ticktime) + (time - prevtime)) / 100.0
		end
		prevtime = time

		oUF.unit.player.energyticking = oUF.unit.player.energy ~= maxen
		if(UnitMana(unit) == maxen) then oUF.unit.player.Tick:Hide() else oUF.unit.player.Tick:Show() end
		oUF.unit.player.var.maxValue = GetTime() + oUF.unit.player.ticktime
		oUF.unit.player.var.startTime = time
	end
end

function oPlayer:OnUpdate()
--	local n = this.var.maxValue - GetTime()
	local n = GetTime() - oUF.unit.player.var.startTime

	local w = oUF.unit.player.Power:GetWidth()
	local sp = ((n) / (oUF.unit.player.var.maxValue - oUF.unit.player.var.startTime)) * w
	if( sp < 0 ) then sp = 0 end
	if( sp > w ) then sp = w end
	oUF.unit.player.Tick:SetPoint("CENTER", oUF.unit.player, "LEFT", sp, 0)
end

function oPlayer:disableBlizzard()
	G["PlayerFrame"]:UnregisterAllEvents()
	G["PlayerFrame"]:Hide()
end
